﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using ComputerVision.ANN;

namespace ComputerVision.Common
{
    public class ImageProcess
    {
        public ImageProcess()
        {
            

        }

        public static Bitmap ConvertBytesToImg(byte[] bytes)
        {
            Bitmap newBitmap = null;
            try
            {
                var mstream = new MemoryStream(bytes, 0, bytes.Length);
                mstream.Write(bytes, 0, bytes.Length);
                newBitmap = new Bitmap(mstream);
                mstream.Flush();
                mstream.Dispose();
            }
            catch (Exception)
            {
            }

            return newBitmap;

        }

        public static byte[] ConvertToByteArray(Bitmap bit)
        {
            var converter = new ImageConverter();
            return (byte[])converter.ConvertTo(bit, typeof(byte[]));
        }

        public static byte[] ConvertToByteArray(Image bit)
        {
            var converter = new ImageConverter();
            return (byte[])converter.ConvertTo(bit, typeof(byte[]));
        }

        public class ImageConvert : System.Windows.Forms.AxHost
        {
            private ImageConvert()
                : base(string.Empty)
            {
            }

            public static Bitmap GetImageFromIPicture(stdole.StdPicture image)
            {
                return (Bitmap)((Image)GetPictureFromIPicture(image));
            }
        }
    }
}
